package com.eric.windows.viewAnimation;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.eric.windows.R;

import java.util.ArrayList;

public class ImageSwitcherActivity extends Activity  {

    private GridView imageGrid;
    private ImageSwitcher imageSwitcher;
    private ArrayList<DataItem> items = new ArrayList<DataItem>();
    private int ImageId[] = {R.mipmap.pic_1,R.mipmap.pic_2,
            R.mipmap.pic_3,R.mipmap.pic_4,
            R.mipmap.pic_5,R.mipmap.pic_1};
    private TextSwitcher textSwitcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_switcher);

        imageGrid = (GridView) findViewById(R.id.image_grid);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);

        textSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);

        for (int i = 0; i < ImageId.length; i++) {
            DataItem  item = new DataItem() ;
            item.drawable = getDrawable(ImageId[i]);
            items.add(item);
        }


        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView img = new ImageView(ImageSwitcherActivity.this);
                ImageSwitcher.LayoutParams layoutParams = new ImageSwitcher.LayoutParams(600,600);
                img.setLayoutParams(layoutParams);
                img.setScaleType(ImageView.ScaleType.FIT_XY);
                return img;
            }
        });

        imageSwitcher.setImageResource(R.mipmap.ic_launcher);

        GridBaseAdapter adapter = new GridBaseAdapter(this,items,1);
        imageGrid.setAdapter(adapter);

        imageGrid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                imageSwitcher.setImageResource(ImageId[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        imageGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                imageSwitcher.setImageResource(ImageId[position]);
            }
        });


        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView img = new TextView(ImageSwitcherActivity.this);
                ImageSwitcher.LayoutParams layoutParams = new ImageSwitcher.LayoutParams(
                        ImageSwitcher.LayoutParams.WRAP_CONTENT,ImageSwitcher.LayoutParams.WRAP_CONTENT);
                img.setLayoutParams(layoutParams);
                img.setText("疯狂的小石头");
                return img;
            }
        });

    }

}
